package com.sjb.sixteen_journey_backend.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sjb.sixteen_journey_backend.entity.*;
import com.sjb.sixteen_journey_backend.entity.vo.CommonessVo;
import com.sjb.sixteen_journey_backend.mapper.CommonnessMapper;
import com.sjb.sixteen_journey_backend.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Component
public class CommonHandle {

    @Autowired
    private ICommentService commentService;


    @Autowired
    private CommonnessMapper commonnessMapper;

    @Autowired
    private IMyTimeService myTimeService;

    @Autowired
    private IAirTicketService airTicketService;

    @Autowired
    private IHotelTicketService hotelTicketService;

    @Autowired
    private IInsuranceTicketService insuranceTicketService;

    @Autowired
    private IOthersTicketService othersTicketService;

    @Autowired
    private IShareThingService shareThingService;

    @Autowired
    private IMyMapService myMapService;

    @Autowired
    private ImagesProperties imagesProperties;

    /**
     * 把一组图片加入到本地
     *
     * @param images
     * @param path
     * @return
     * @throws IOException
     */
//    public List<String> saveImages(List<MultipartFile> images, String path) throws IOException {
//        List<String> files = new ArrayList<>();
//        for (MultipartFile image : images) {
//            String originalFilename = image.getOriginalFilename();
//            String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//            String realPath = System.getProperty("user.dir") + imagesProperties.getRootPath() + path;
//            image.transferTo(new File(realPath + filename));
//            files.add(imagesProperties.getVisitImagePath() + imagesProperties.getAvatar() + filename);
//        }
//        return files;
//    }

    /**
     * 把一张图片加入到本地
     * @param image
     * @param path
     * @return
     * @throws IOException
     */
//    public boolean saveImage(String image, String path) throws IOException {
//        MultipartFile file = new MultipartFile();
//        String filename = UUID.randomUUID().toString() + image.substring(image.lastIndexOf("."));
//        String realPath = System.getProperty("user.dir") + imagesProperties.getRootPath() + path;
//        file.transferTo(new File(realPath + filename));
//        return true;
//    }

    /**
     * 把图片路径字符串转换成List
     *
     * @param path
     * @return
     * @throws IOException
     */
    public List<String> imageByStringToList(String path) throws IOException {
        List<String> imagePaths = Arrays.asList(path.substring(1, path.length() - 1).split(", "));
        return imagePaths;
    }


    /**
     * 删除关联表
     *
     * @param path
     * @param status
     * @param id
     * @return
     */
    public Boolean deleteAssociationTable(String path, Integer status, Integer id, String name) {
        try {
//            目前用的是阿里云所以不用下面的代码
            //在本地删除图片
//            List<String> strings = this.imageByStringToList(path);
//            for (String s : strings) {
//                String img = s.substring(s.lastIndexOf('/') + 1);
//                String realPath = System.getProperty("user.dir") + imagesProperties.getRootPath() + name;
//                // 构建文件对象
//                File file = new File(realPath + img);
//                file.delete();
//            }

            //删除有关的评论
            QueryWrapper<Comment> wrapper = Wrappers.query();
            wrapper.eq("module_status", status);
            wrapper.eq("module_id", id);
            commentService.remove(wrapper);

            // 删除有关被收藏，被喜欢的记录
            HashMap<String, Object> param = new HashMap<>();
            param.put("status", status);
            param.put("module_id", id);
            commonnessMapper.deleteByMap(param);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 判断文章是否存在
     *
     * @param status
     * @param id
     * @return
     */
    public Boolean determineIsExistTicket(Integer status, Integer id) {
        switch (status) {
            case 0: {
                MyTime byId = myTimeService.getById(id);
                if (byId == null) {
                    return false;
                } else return true;
            }

            case 1: {
                AirTicket byId = airTicketService.getById(id);
                if (byId == null) {
                    return false;
                } else return true;
            }

            case 2: {
                HotelTicket byId = hotelTicketService.getById(id);
                if (byId == null) {
                    return false;
                } else return true;
            }
            case 3: {
                InsuranceTicket byId = insuranceTicketService.getById(id);
                if (byId == null) {
                    return false;
                } else return true;
            }
            case 4: {
                OthersTicket byId = othersTicketService.getById(id);
                if (byId == null) {
                    return false;
                } else return true;
            }
            case 5: {
                ShareThing byId = shareThingService.getById(id);
                if (byId == null) {
                    return false;
                } else return true;
            }
            case 6: {
                MyMap byId = myMapService.getById(id);
                if (byId == null) {
                    return false;
                } else return true;
            }
            default:
                return false;
        }
    }

}
